// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.

import org.codehaus.groovy.runtime.IOGroovyMethods

suite ("test_base") {
    // this mv rewrite would not be rewritten in RBO phase, so set TRY_IN_RBO explicitly to make case stable
    sql "set pre_materialized_view_rewrite_strategy = TRY_IN_RBO"
    sql """set enable_nereids_planner=true"""
    sql """SET enable_fallback_to_original_planner=false"""
    sql """ drop table if exists dwd;"""

    sql """
        CREATE TABLE `dwd` (
            `id` bigint(20) NULL COMMENT 'id',
            `created_at` datetime NULL,
            `dt` date NULL
            ) ENGINE=OLAP
            DUPLICATE KEY(`id`)
            DISTRIBUTED BY HASH(`id`) BUCKETS 10
            PROPERTIES (
            "replication_allocation" = "tag.location.default: 1"
        );
        """

    sql """insert into dwd(id) values(1);"""

    createMV ("""
            create materialized view dwd_mv as SELECT created_at as a1, id  as a2 FROM dwd;
    """)

    sql """insert into dwd(id) values(2);"""

    sql "analyze table dwd with sync;"
    sql """alter table dwd modify column id set stats ('row_count'='2');"""
    sql """set enable_stats=false;"""

    mv_rewrite_success("SELECT created_at, id  FROM dwd where created_at = '2020-09-09 00:00:00' order by 1, 2;", "dwd_mv")
    qt_select_mv "SELECT created_at, id FROM dwd order by 1, 2;"

    mv_rewrite_success("SELECT id,created_at  FROM dwd where id is not null order by 1, 2;", "dwd_mv")
    qt_select_mv "SELECT id,created_at FROM dwd order by 1, 2;"

    sql """set enable_stats=true;"""
    mv_rewrite_success_without_check_chosen("SELECT created_at, id  FROM dwd where created_at = '2020-09-09 00:00:00' order by 1, 2;",
            "dwd_mv", [TRY_IN_RBO, NOT_IN_RBO])
    mv_rewrite_success_without_check_chosen("SELECT created_at, id  FROM dwd where created_at = '2020-09-09 00:00:00' order by 1, 2;",
            "dwd_mv", [FORCE_IN_RBO])

    mv_rewrite_success_without_check_chosen("SELECT id,created_at  FROM dwd where id is not null order by 1, 2;",
            "dwd_mv", [TRY_IN_RBO, NOT_IN_RBO])
    mv_rewrite_success_without_check_chosen("SELECT id,created_at  FROM dwd where id is not null order by 1, 2;",
            "dwd_mv", [FORCE_IN_RBO])
}
